//
//  CommonClassReportHeadView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2018/1/29.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "CommonClassReportHeadView.h"

@implementation CommonClassReportHeadView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        UIButton *leftBtn = [[UIButton alloc] init];
        [leftBtn setImage:@"icn_left_arrow"];
        [leftBtn addTarget:self action:@selector(timeBtnClick:)];
        leftBtn.tag = 0;
        [self addSubview:leftBtn];
        self.leftBtn = leftBtn;
        
        UIButton *rightBtn = [[UIButton alloc] init];
        [rightBtn setImage:@"icn_right_arrow"];
        [rightBtn addTarget:self action:@selector(timeBtnClick:)];
        rightBtn.tag = 1;
        [self addSubview:rightBtn];
        self.rightBtn = rightBtn;
        
        UILabel *timeLabel1 = [[UILabel alloc] init];
        timeLabel1.textColor = CSColorFromRGB(0x0a0a0a);
        timeLabel1.font = MidBigFont;
        [self addSubview:timeLabel1];
        self.timeLabel1 = timeLabel1;
        
        UILabel *timeLabelUnit = [[UILabel alloc] init];
        timeLabelUnit.textColor = CSColorFromRGB(0x0a0a0a);
        timeLabelUnit.font = MidBigFont;
        timeLabelUnit.text = @"至";
        [self addSubview:timeLabelUnit];
        self.timeLabelUnit = timeLabelUnit;
        
        UILabel *timeLabel2 = [[UILabel alloc] init];
        timeLabel2.textColor = CSColorFromRGB(0x0a0a0a);
        timeLabel2.font = MidBigFont;
        [self addSubview:timeLabel2];
        self.timeLabel2 = timeLabel2;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [self addSubview:lineView];
        self.lineView = lineView;
        
        /**
         今日按钮
         */
        UIButton *todayBtn = [[UIButton alloc] init];
        todayBtn.tag = 1;
        [todayBtn addTarget:self action:@selector(shortTimeBtnClick:)];
        todayBtn.layer.cornerRadius = 2;
        todayBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        todayBtn.layer.borderWidth = 1.0;
        todayBtn.layer.masksToBounds = YES;
        todayBtn.titleLabel.font = MidFont;
        todayBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        [todayBtn setTitle:@"今日"];
        [todayBtn setTitleColor:CSColorFromRGB(0x333333)];
        [self addSubview:todayBtn];
        self.todayBtn = todayBtn;
        
        
        /**
         最近7天
         */
        UIButton *weekBtn = [[UIButton alloc] init];
        weekBtn.tag = 2;
        [weekBtn addTarget:self action:@selector(shortTimeBtnClick:)];
        weekBtn.layer.cornerRadius = 2;
        weekBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        weekBtn.layer.borderWidth = 1.0;
        weekBtn.layer.masksToBounds = YES;
        weekBtn.titleLabel.font = MidFont;
        weekBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        [weekBtn setTitle:@"最近7天"];
        [weekBtn setTitleColor:CSColorFromRGB(0x333333)];
        [self addSubview:weekBtn];
        self.weekBtn = weekBtn;
        
        
        /**
         最近30天
         */
        UIButton *monBtn = [[UIButton alloc] init];
        [monBtn addTarget:self action:@selector(shortTimeBtnClick:)];
        monBtn.tag = 3;
        monBtn.layer.cornerRadius = 2;
        monBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        monBtn.layer.borderWidth = 1.0;
        monBtn.layer.masksToBounds = YES;
        monBtn.titleLabel.font = MidFont;
        monBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        [monBtn setTitle:@"最近30天"];
        [monBtn setTitleColor:CSColorFromRGB(0x333333)];
        [self addSubview:monBtn];
        self.monBtn = monBtn;
        
        /**
         刷选
         */
        UIButton *fitBtn = [[UIButton alloc] init];
        fitBtn.backgroundColor = CSWhiteColor;
        [fitBtn setTitle:@"筛选"];
        [fitBtn setTitleColor:CSColorFromRGB(0x666666)];
        [fitBtn setSelectedTitleColor:CSColorFromRGB(0x0a0a0a)];
        [fitBtn setImage:@"icon_select"];
        fitBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [self addSubview:fitBtn];
        self.fitBtn = fitBtn;
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSDate *date = [NSDate date];
        NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *dateComponents = [calendar components:NSCalendarUnitDay fromDate:date];
        self.timeLabel1.text = [formatter stringFromDate:date];
        [dateComponents setMonth:+1];
        NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
        self.timeLabel2.text = [formatter stringFromDate:newdate];
        
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.leftBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.timeLabelUnit);
        
        make.left.mas_equalTo(self).offset(20);
    }];
    
    [self.rightBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.timeLabelUnit);
        
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    [self.timeLabel1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.timeLabelUnit);
        
        make.right.mas_equalTo(self.timeLabelUnit.mas_left);
    }];
    
    [self.timeLabelUnit mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self);
        
        make.top.mas_equalTo(self).offset(15);
    }];
    
    //时间
    [self.timeLabel2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.timeLabelUnit);
        
        make.left.mas_equalTo(self.timeLabelUnit.mas_right);
    }];
    
    //线条
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.timeLabelUnit.mas_bottom).offset(30);
        
        make.left.width.mas_equalTo(self);
        
        make.height.mas_equalTo(1);
    }];
    
    [self.todayBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(20);
        
        make.left.mas_equalTo(self).offset(10);
        
        make.width.mas_equalTo(self.monBtn);
        
        make.height.mas_equalTo(40);
    }];
    
    [self.weekBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.todayBtn);
        
        make.left.mas_equalTo(self.todayBtn.mas_right).offset(15);
        
        make.width.mas_equalTo(self.monBtn);
        
        make.height.mas_equalTo(40);
    }];
    
    [self.monBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.todayBtn);
        
        make.left.mas_equalTo(self.weekBtn.mas_right).offset(15);
        
        make.width.mas_equalTo((SCREEN_WIDTH - 20) / 4);
        
        make.height.mas_equalTo(40);
        
        make.bottom.mas_equalTo(self).offset(-20);
    }];
    
    [self.fitBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.monBtn);
        
        make.right.mas_equalTo(self).offset(-10);
    }];
}

#pragma mark 快速日期选择
-(void)shortTimeBtnClick:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    NSDate *date = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitDay fromDate:date];
    
    if (tag == 1) {
        
        self.todayBtn.backgroundColor = ButtonBackGroundColor;
        self.todayBtn.layer.borderColor = ButtonBackGroundColor.CGColor;
        self.weekBtn.backgroundColor = CSWhiteColor;
        self.weekBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        self.monBtn.backgroundColor = CSWhiteColor;
        self.monBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        self.timeLabel1.text = [formatter stringFromDate:date];
        self.timeLabel2.text = [formatter stringFromDate:date];
    }
    else if (tag == 2){
        
        self.todayBtn.backgroundColor = CSWhiteColor;
        self.weekBtn.backgroundColor = ButtonBackGroundColor;
        self.monBtn.backgroundColor = CSWhiteColor;
        
        self.weekBtn.layer.borderColor = ButtonBackGroundColor.CGColor;
        self.todayBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        self.monBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        [dateComponents setDay:-12];
        NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
        self.timeLabel1.text = [formatter stringFromDate:newdate];
        self.timeLabel2.text = [formatter stringFromDate:date];
    }
    else{
        
        self.todayBtn.backgroundColor = CSWhiteColor;
        self.weekBtn.backgroundColor = CSWhiteColor;
        self.monBtn.backgroundColor = ButtonBackGroundColor;
        
        self.monBtn.layer.borderColor = ButtonBackGroundColor.CGColor;
        self.todayBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        self.weekBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        
        [dateComponents setDay:-30];
        NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
        self.timeLabel1.text = [formatter stringFromDate:newdate];
        self.timeLabel2.text = [formatter stringFromDate:date];
    }
    
    if ([self.commonClassReporHeadViewDelegate respondsToSelector:@selector(operateClick)]) {
        
        [self.commonClassReporHeadViewDelegate operateClick];
    }
}


#pragma mark 做成
-(void)timeBtnClick:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    
    
    //坐变
    if (tag == 0) {
        
        NSString *time1 = self.timeLabel1.text;
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        
        NSDate *date = [formatter dateFromString:time1];
        
        NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        
        NSDateComponents *dateComponents = [calendar components:NSCalendarUnitDay fromDate:date];
        
        [dateComponents setMonth:-1];
        
        NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
        
        self.timeLabel1.text = [formatter stringFromDate:newdate];
        
        self.timeLabel2.text = time1;
    }
    else{
        
        NSString *time2 = self.timeLabel2.text;
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        
        NSDate *date = [formatter dateFromString:time2];
        
        NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        
        NSDateComponents *dateComponents = [calendar components:NSCalendarUnitDay fromDate:date];
        
        [dateComponents setMonth:1];
        
        NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
        
        self.timeLabel2.text = [formatter stringFromDate:newdate];
        
        self.timeLabel1.text = time2;
    }
    
    if ([self.commonClassReporHeadViewDelegate respondsToSelector:@selector(operateClick)]) {
        
        [self.commonClassReporHeadViewDelegate operateClick];
    }
}

-(void)layoutSubviews{
    
    /**
     刷选
     */
    [self.fitBtn imageOnTheTitleLeftWithSpace:5];
}

@end
